/**
 * Created by Administrator on 2016/7/11.
 */
"use strict";
swan_pc
    .factory("CompanyClipSearch",function($resource,Clip){
        return {
            companyClip:function(id,option,CallBack){
                if(id==null||id==undefined){
                    return "";
                }
                var clips=[];
                var companyClip=[];
                var oppClip=[];
                var predicate = 'id';
                var reverse = true;
                var page = 1;
                Clip.query({page: page - 1, size: 500, sort: [predicate + ',' + (reverse ? 'asc' : 'desc'), 'id']}, function(result, headers) {
                    var totalItems = headers('X-Total-Count');
                    clips = result;
                    //console.log("clips"+clips);
                    for(var i=0;i<clips.length;i++){
                        if(clips[i].relatedCompany!=""&&clips[i].relatedCompany!=null){
                            var str = clips[i].relatedCompany.split(":");
                            //console.log("str:"+str[0]);
                            var relatedCompanyId = parseInt(str[0]);
                            //console.log("compare:"+relatedCompanyId+"   "+id);
                            if (relatedCompanyId == id) {
                                //console.log("push is run");
                                companyClip.push(clips[i]);
                                //console.log("companyClip.length:"+companyClip.length);
                            }
                        }
                        /*if(clips[i].relatedOpp!=""&&clips[i].relatedOpp!=null){
                            var str = clips[i].relatedOpp.split(":");
                            //console.log("str:"+str[0]);
                            var relatedOppId = parseInt(str[0]);
                            console.log("compare:"+relatedOppId+"   "+id);
                            if (relatedOppId == id) {
                                console.log("opp push is run");
                                oppClip.push(clips[i]);
                            }
                        }*/
                    }
                    //console.log("companyClip"+companyClip);
                    CallBack(companyClip);
                    if(option==0){
                        //console.log(companyClip);
                        console.log("companyClip.length:"+companyClip.length);
                        CallBack(companyClip);
                    }else{
                        return oppClip;
                    }
                })

            }
        }
    })
